<?php session_start(); require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=kontrak";
$ope = $_POST['open'];
// -- validasi
if($act <>'delete') { 
	if (trim($_POST['noreferensi']) == '') {
		$error[] = '- No Kontrak harus diisi !!!';
	}
	// cek kontrak
	mysql_select_db($database_con_gl, $con_gl);
	$query_cekpk = "select no from gl_kontrak where no='$_POST[noreferensi]'";
	$cekpk       = mysql_query($query_cekpk, $con_gl) or die(mysql_error());
	$total_cekpk = mysql_num_rows($cekpk);
	if($total_cekpk > 0 AND $act=='add') {
		$error[] = '- No. Kontak <b>'.$_POST[noreferensi].'</b> sudah digunakan !!!';
	}
	//----
	if ((trim($_POST['tanggal']) == '')) {
		$error[] = '- Tanggal harus diisi !!!';
	}
	if (trim($_POST['dari']) == '') {
		$error[] = '- Customer harus dipilih !!!';
	}
	if ((trim($_POST['awal']) == '') OR (trim($_POST['akhir']) == '')) {
		$error[] = '- Periode Kontrak harus diisi !!!';
	}
	if (trim($_POST['durasi']) == '') {
		$error[] = '- Durasi Kontrak harus dipilih !!!';
	}
	if (trim($_POST['pembayaran']) == '') {
		$error[] = '- Periode Pembayaran harus dipilih !!!';
	}
	/*if ($_POST['ntotal'] <= 0) {
		$error[] = '- Periksa Total Transaksi anda !!!';
	}*/
}
/*End validasi */
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	switch($act) {
	case("delete"):
		$query = "delete from gl_kontrak where no='$_POST[noreferensi]'";
		$query2 = "delete from gl_dkontrak where kontrak='$_POST[noreferensi]'";
		$query3 = "delete from gl_retailtrans where no_ref = '$_POST[noreferensico]'";
		$query4 = "delete from gl_detailtrans where no_ref = '$_POST[noreferensico]'";
		
		// hapus data
		mysql_select_db($database_con_gl, $con_gl);
		
		$runquery = mysql_query($query, $con_gl);
		$runquery = mysql_query($query2, $con_gl);
		$runquery = mysql_query($query3, $con_gl);
		$runquery = mysql_query($query4, $con_gl);
		mysql_query("delete from gl_pembayaran where no_ref = '$_POST[noreferensico]'") or die (mysql_error());			
		if($runquery) { 
			// ---
			mysql_query($query2, $con_gl);
			
			// ---
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil dihapus ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal dihapus !!!";
		}
	break;
	case("edit"):
		mysql_select_db($database_con_gl, $con_gl);
		$pajak = $_SESSION['subtotal']*($_POST['pajak']/100);
		$pph = $_SESSION['subtotal']*($_POST['pph']/100);
		$grandtotal = $_SESSION['subtotal']+$_SESSION['tbiaya']+($_SESSION['subtotal']*$_POST['pajak']/100) - $pph;
		$query="update gl_kontrak set nopo='$_POST[nopo]', kontak='$_POST[dari]', durasi='$_POST[durasi]', pbayar='$_POST[pembayaran]',  tgl='$_POST[tanggal]', mulai='$_POST[awal]', sampai='$_POST[akhir]', info='$_POST[keterangan]', biaya='$_SESSION[tbiaya]', pph='$pph',pajak='$pajak', total='$grandtotal', recipient='$_POST[recipient]' where no='$_POST[noreferensi]'";
		
		//-- save data
		
		// edit retailtrans
		$query_retail = "update gl_retailtrans set tgl='$_POST[awal]',keterangan='$_POST[keterangan]',kontak='$_POST[dari]',jenis='$_POST[jenis]',gudang='$_POST[idgudang]',area='$_POST[idarea]',jatuhtempo='$_POST[akhir]',checkin='$_POST[awal]',checkout='$_POST[akhir]',updated_by = '$_SESSION[nama]', updated_at = '$today',recipient='$_POST[recipient]' where kontrak ='$_POST[noreferensi]'";
		//hapus data pada gl_dkontrak
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		$runqueryretail = mysql_query($query_retail, $con_gl) or die (mysql_error());
		mysql_query("delete from gl_dkontrak where kontrak = '$_POST[noreferensi]'") or die (mysql_error());
		mysql_query("delete from gl_detailtrans where no_ref = '$_POST[noreferensico]'") or die (mysql_error());
	
	
		$i=0;
		
		if(isset($_SESSION['kendaraan'])){
			foreach($_SESSION['kendaraan'] as $kd){
				$harga 	= $_SESSION['hargasatuan'][$i];
				$diskon	= $_SESSION['diskon'][$i];
				$biaya	= $_SESSION['biaya'][$i];
				$diskonamount	= $harga * ($diskon /100);
				
				$total	= $_SESSION['total'][$i];
				$transaksi	= $_SESSION['transaksi'][$i];
				$ppn = $total*($_POST['pajak']/100);
				$pph = $total*($_POST['pph']/100);
				$runquery = mysql_query("insert into gl_dkontrak(kontrak,kendaraan,harga,diskon,biayatambahan,total,transaksi) values ('$_POST[noreferensi]','$kd','$harga','$diskon','$biaya','$total','$transaksi');") or die (mysql_error());
				
				// ini untuk detail trans
				$addquery = mysql_query("INSERT INTO gl_detailtrans (id, no_ref, transaksi, total, barang, kendaraan, ppn,pph, hargasatuan, diskon,biayatambahan,diskonamount) VALUES (NULL, '$_POST[noreferensico]', '$_POST[transaksi]', '$total', '$_POST[barang]', '$kd', '$ppn','$pph','$harga', '$diskon','$biaya','$diskonamount');") or die (mysql_error());
					mysql_query("delete from gl_pembayaran where no_ref = '$_POST[noreferensico]'") or die (mysql_error());			
				$i++;
			}
		}
		
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		if($runquery && $runqueryretail && $addquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
		}
	break;
	default:
		// ---
		list($area) = mysql_fetch_row(mysql_query("select area from gl_gudang where id = '$_POST[gudang]'"));
		$pajak = $_SESSION['subtotal']*($_POST['pajak']/100);
		$pph = $_SESSION['subtotal']*($_POST['pph']/100);
		$grandtotal = $_SESSION['subtotal']+$_SESSION['tbiaya']+($_SESSION['subtotal']*$_POST['pajak']/100) - $pph;
		$query = "insert into gl_kontrak values ('$_POST[noreferensi]','$_POST[nopo]' ,'$_POST[dari]','$_POST[durasi]','$_POST[pembayaran]','$_POST[idarea]','$_POST[idgudang]','$_POST[tanggal]','$_POST[awal]','$_POST[akhir]','$_POST[keterangan]','$_SESSION[tbiaya]','$pajak','$pph','$grandtotal','$_POST[recipient]')";
		// save data kontrak
		
		$query_retail = "insert into gl_retailtrans (no_ref,jnsorder,tgl,keterangan,kontak,jenis,gudang,area,jatuhtempo,kontrak,checkin,checkout,recipient,created_by,created_at) values ('$_POST[noreferensico]','1','$_POST[awal]','$_POST[keterangan]','$_POST[dari]','$_POST[jenis]','$_POST[idgudang]','$_POST[idarea]','$_POST[akhir]','$_POST[noreferensi]','$_POST[awal]','$_POST[akhir]','$_POST[recipient]','$_SESSION[nama]','$today')";

		
		mysql_select_db($database_con_gl, $con_gl);
		$runquery = mysql_query($query, $con_gl) or die (mysql_error());
		$runqueryretail = mysql_query($query_retail, $con_gl) or die (mysql_error());
		
		//Save to gl_retailtrans
		$i=0;
		if(isset($_SESSION['kendaraan'])){
			foreach($_SESSION['kendaraan'] as $kd){
				$harga 	= $_SESSION['hargasatuan'][$i];
				$diskon	= $_SESSION['diskon'][$i];
				$biaya	= $_SESSION['biaya'][$i];
				$diskonamount	= $harga * ($diskon /100);
				
				$total	= $_SESSION['total'][$i];
				$transaksi	= $_SESSION['transaksi'][$i];
				$ppn = $total*($_POST['pajak']/100);
				$pph = $total*($_POST['pph']/100);
				$runquery = mysql_query("insert into gl_dkontrak(kontrak,kendaraan,harga,diskon,biayatambahan,total,transaksi) values ('$_POST[noreferensi]','$kd','$harga','$diskon','$biaya','$total','$transaksi');") or die (mysql_error());
				
				// ini untuk detail trans
				$addquery = mysql_query("INSERT INTO gl_detailtrans (id, no_ref, transaksi, total, barang, kendaraan, ppn,pph, hargasatuan, diskon,biayatambahan,diskonamount) VALUES (NULL, '$_POST[noreferensico]', '$_POST[transaksi]', '$total', '$_POST[barang]', '$kd', '$ppn','$pph','$harga', '$diskon','$biaya','$diskonamount');") or die (mysql_error());
				$i++;
			}
		}
		if($runquery && $runqueryretail && $addquery) { 
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil disimpan ...";
			echo "<script type=\"text/javascript\">setTimeout(\"location.href='".$url."'\", 2000);</script>";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";
			mysql_query("delete from gl_kontrak where no = '$_POST[noreferensi]'") or die (mysql_error());
			mysql_query("delete from gl_dkontrak where kontrak = '$_POST[noreferensi]'") or die (mysql_error());
			mysql_query("delete from gl_retailtrans where no_ref = '$_POST[noreferensico]'") or die (mysql_error());
			mysql_query("delete from gl_detailtrans where no_ref = '$_POST[noreferensico]'") or die (mysql_error());
		}
	break;
	}
}
?>